﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Bll;
using System.Data;
using System.Web.Security;

public partial class StudentInfo : System.Web.UI.Page
{
    StudentBll bll = new StudentBll();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            thispower();
            
        }
        BindGrid();
    }
    private void thispower()
    {
        if (Session["usergroup"] != null && Session["usergroup"] != "")
        {
            if (Session["usergroup"] != "admin")
            {
                Response.Write("<script>alert(\"你无权查看！\");history.back();</script>");
            }
        }
        else
        {
            Response.Write("<script>alert(\"你无权查看！\");history.back();</script>");
        }
    }
    protected void lbtn1_Click(object sender, EventArgs e)
    {
        BindGrid();
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    private void BindGrid()
    {
        string keyword = TextBox1.Text.Trim();
        GridView1.DataSource = bll.getStudents(keyword);
        GridView1.DataBind();
    }
    protected void lbtn2_Click(object sender, EventArgs e)
    {
        string sno = ((LinkButton)sender).CommandArgument;
        if (bll.enable(sno) > 0)
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>alert('设置成功');</script>");
        }
        else
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>alert('设置失败');</script>");
        }
        BindGrid();
    }
    protected string getname(string sno)
    {
        int en = bll.getisok(sno);
        if (en == 0)
        {
            return "解封";
        }
        else
        {
            return "封号";
        }
    }
    protected void lbtnPwd_Click(object sender,EventArgs e)
    {
        string sno = Label1.Text;
        string pwd = FormsAuthentication.HashPasswordForStoringInConfigFile(TextBox2.Text.Trim(),"md5");
        string sql = "update Student set spwd='"+pwd+"' where sno='"+sno+"'";
        if (bll.ExcecuteSql(sql) > 0)
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>alert('设置成功');</script>");
            GridView1.Visible = true;
            dialog.Visible = false;
        }
        else
        {
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "myscript", "<script>alert('设置失败');</script>");
        }
    }
    protected void lbtn3_Click(object sender, EventArgs e)
    {
        string id = ((LinkButton)sender).CommandArgument;
        GridView1.Visible = false;
        dialog.Visible = true;
        string sql = "select * from Student where sno='" + id + "'";
        DataTable dt = bll.GetDataTable(sql);
        if (dt.Rows.Count > 0)
        {
            Label1.Text = dt.Rows[0]["sno"].ToString();
            Label2.Text = dt.Rows[0]["sname"].ToString();
        }
        
    }
    protected void lbtnCancle_Click(object sender, EventArgs e)
    {
        GridView1.Visible = true;
        dialog.Visible = false;
    }
}